package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.domain.YhNotice;
import com.ruoyi.system.mapper.YhNoticeMapper;
import com.ruoyi.system.service.IYhNoticeService;
import org.springframework.stereotype.Service;

/**
 * 各类型公告展示Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-06
 */
@Service
public class YhNoticeServiceImpl extends ServiceImpl<YhNoticeMapper, YhNotice> implements IYhNoticeService
{

    @Override
    public YhNotice selectByType(String type) {
        return getOne(new LambdaQueryWrapper<YhNotice>().eq(YhNotice::getType, type));
    }

    @Override
    public boolean editById(YhNotice yhNotice) {
        YhNotice byId = this.getById(yhNotice.getId());
        if(StringUtils.isNotEmpty(byId.getVideo()) && !byId.getVideo().equals(yhNotice.getVideo())){
            // 删除原文件
            String path = RuoYiConfig.getProfile() + byId.getVideo().replaceAll(Constants.RESOURCE_PREFIX, "");
            FileUploadUtils.remove(path);
        }
        if(StringUtils.isNotEmpty(byId.getImgUrl()) && !byId.getImgUrl().equals(yhNotice.getImgUrl())){
            // 删除原文件
            String path = RuoYiConfig.getProfile() + byId.getImgUrl().replaceAll(Constants.RESOURCE_PREFIX, "");
            FileUploadUtils.remove(path);
        }

        return this.updateById(yhNotice);
    }
}
